﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeFarmer.Model.Entities;
using CodeFarmer.DataAccess.Infrastructure;
using CodeFarmer.DataAccess;

namespace CodeFarmer.Service
{
    public interface IUserService
    {
        User GetUser(int UserID);
        User GetUser(string UserName);
        int AddUser(User user);
        Boolean ValidateUser(string userName, string passWord);
        Boolean ExistEmail(string email);
        Boolean ExistUserName(string userName);
        Boolean ExistNickName(string nickName);
    }

    public class UserService : IUserService
    {
        private readonly IUserRepository userRepository;
        private readonly IUnitOfWork unitOfWork;

        public UserService(IUnitOfWork unitOfWork, IUserRepository userRepository)
        {
            this.unitOfWork = unitOfWork;
            this.userRepository = userRepository;
        }

        private int Save()
        {
           return unitOfWork.Save();
        }

        public User GetUser(int UserID)
        {
            return userRepository.GetById(UserID);
        }

        public User GetUser(string userName)
        {
            return userRepository.GetUser(userName);
        }

        public int AddUser(User user)
        {
            try
            {
                userRepository.Add(user);
                Save();
                return user.ID;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        public Boolean ValidateUser(string userName, string passWord) 
        {
            return userRepository.ValidateUser(userName, passWord);
        }

        public Boolean ExistEmail(string email)
        {
            return userRepository.ExistEmail(email);
        }

        public Boolean ExistUserName(string userName)
        {
            return userRepository.ExistUserName(userName);
        }

        public Boolean ExistNickName(string nickName)
        {
            return userRepository.ExistNickName(nickName);
        }
    }
}
